package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.CreditServiceConfigMapper;
import com.ruoyi.system.domain.CreditServiceConfig;
import com.ruoyi.system.domain.CreditUserInfo;
import com.ruoyi.system.service.ICreditServiceConfigService;
import com.ruoyi.system.service.ICreditUserInfoService;

/**
 * 服务配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-28
 */
@Service
public class CreditServiceConfigServiceImpl implements ICreditServiceConfigService 
{
    @Autowired
    private CreditServiceConfigMapper creditServiceConfigMapper;

    @Autowired
    private ICreditUserInfoService creditUserInfoService;

    /**
     * 查询服务配置
     * 
     * @param configId 服务配置主键
     * @return 服务配置
     */
    @Override
    public CreditServiceConfig selectCreditServiceConfigByConfigId(Long configId)
    {
        return creditServiceConfigMapper.selectCreditServiceConfigByConfigId(configId);
    }

    /**
     * 查询服务配置列表
     * 
     * @param creditServiceConfig 服务配置
     * @return 服务配置
     */
    @Override
    public List<CreditServiceConfig> selectCreditServiceConfigList(CreditServiceConfig creditServiceConfig)
    {
        return creditServiceConfigMapper.selectCreditServiceConfigList(creditServiceConfig);
    }

    /**
     * 新增服务配置
     * 
     * @param creditServiceConfig 服务配置
     * @return 结果
     */
    @Override
    public int insertCreditServiceConfig(CreditServiceConfig creditServiceConfig)
    {
        creditServiceConfig.setCreateTime(DateUtils.getNowDate());
        return creditServiceConfigMapper.insertCreditServiceConfig(creditServiceConfig);
    }

    /**
     * 修改服务配置
     * 
     * @param creditServiceConfig 服务配置
     * @return 结果
     */
    @Override
    public int updateCreditServiceConfig(CreditServiceConfig creditServiceConfig)
    {
        creditServiceConfig.setUpdateTime(DateUtils.getNowDate());
        return creditServiceConfigMapper.updateCreditServiceConfig(creditServiceConfig);
    }

    /**
     * 批量删除服务配置
     * 
     * @param configIds 需要删除的服务配置主键
     * @return 结果
     */
    @Override
    public int deleteCreditServiceConfigByConfigIds(Long[] configIds)
    {
        return creditServiceConfigMapper.deleteCreditServiceConfigByConfigIds(configIds);
    }

    /**
     * 删除服务配置信息
     * 
     * @param configId 服务配置主键
     * @return 结果
     */
    @Override
    public int deleteCreditServiceConfigByConfigId(Long configId)
    {
        return creditServiceConfigMapper.deleteCreditServiceConfigByConfigId(configId);
    }

    /**
     * 根据服务代码查询服务配置
     * 
     * @param serviceCode 服务代码
     * @return 服务配置
     */
    @Override
    public CreditServiceConfig selectCreditServiceConfigByServiceCode(String serviceCode)
    {
        return creditServiceConfigMapper.selectCreditServiceConfigByServiceCode(serviceCode);
    }

    /**
     * 查询可用的服务配置列表
     * 
     * @return 服务配置集合
     */
    @Override
    public List<CreditServiceConfig> selectAvailableServiceConfigs()
    {
        return creditServiceConfigMapper.selectAvailableServiceConfigs();
    }

    /**
     * 校验用户是否符合服务条件
     * 
     * @param userId 用户ID
     * @param serviceCode 服务代码
     * @return 结果
     */
    @Override
    public boolean checkUserServiceEligibility(Long userId, String serviceCode)
    {
        try {
            // 获取服务配置
            CreditServiceConfig serviceConfig = creditServiceConfigMapper.selectCreditServiceConfigByServiceCode(serviceCode);
            if (serviceConfig == null || !"0".equals(serviceConfig.getStatus())) {
                return false; // 服务不存在或已停用
            }

            // 获取用户信用信息
            CreditUserInfo userInfo = creditUserInfoService.selectCreditUserInfoByUserId(userId);
            if (userInfo == null) {
                return false; // 用户信用信息不存在
            }

            // 检查用户信用分是否满足条件
            return userInfo.getCreditScore() >= serviceConfig.getRequiredScore();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
} 